<?php

class ProfileController extends ControllerBase
{

	public function initProfileCard()
	{

		$user_id = $_GET['bhd'];
		$this->session->set('bhd_id',$user_id);
		$feeds = UserFeedsTb::listProfileFeeds($user_id);
		
		$info = UserAccountTb::findFirstById($user_id);
		if($info == false || $this->session->get('user_id') == null)
		{
			header("Location: ".BASE_URI);
		}
		
		$this->view->setVar('feeds',$feeds);
		$this->view->setVar('info',$info);
		$work_experience = json_decode($info->work_experience);		
		$address = UserAddressTb::findFirst(array(
				'conditions'=> "user_id = ?0 AND type = 'home_address'",
				'bind' => array(0=>$user_id)
			));
		$schools = json_decode($info->schools);
		$skills = json_decode($info->skills);
		$profile_pic = (!$info->profile_pic || $info->profile_pic != '') ? IMAGE_URL_SMALL.$info->profile_pic : BASE_URI.'img/icons/ProfileDpMale.png';
		$this->view->setVar('skills',$skills);
		$this->view->setVar('profile_pic',$profile_pic);
		$this->view->setVar("schools",$schools);	
		$this->view->setVar('address',$address);
		$this->view->setVar('work_experience',$work_experience[0]);

		$possible_connections = UserConnectionsTb::listPossibleConnection($user_id);
		$this->view->setVar('possible_connections',$possible_connections);

		$suggested_groups = Groups::listSuggestedGroups($_GET['bhd']);
		$this->view->setVar('suggested_groups',$suggested_groups);
	}

    public function indexAction()
    {
		$this->view->setMainView('index');
		$this->view->setVar('page_name', 'Profile');
		$this->view->setVar('content', 'profile/index');
		$this->view->setVar('title',$this->session->get('fullname'));
		
		$this->initProfileCard();

		
	}
    public function feedsAction()
    {
		$this->view->setMainView('index');
		$this->view->setVar('page_name', 'Feeds');
		$this->view->setVar('content', 'profile/feeds');
	}

	public function groupAction()
	{
		$this->view->setMainView('index');
		$this->view->setVar('page_name', 'Profile');
		$this->view->setVar('content', 'profile/group');
		$this->view->setVar('title','|' . $this->session->get('fullname'));	
		$this->initProfileCard();
	}

	public function myconnectionAction()
	{
		$this->view->setMainView('index');
		$this->view->setVar('page_name', 'Profile');
		$this->view->setVar('content', 'profile/myconnection');	
		
	
		$this->initProfileCard();
		$user_id = $_GET['bhd'];
		$connections = UserConnectionsTb::listConnection($user_id);

		$paginator = new \Phalcon\Paginator\Adapter\Model(
		    array(
		        "data" => $connections,
		        "limit"=> 10,
		        "page" => 1
		    )
		);

		// Get the paginated results
		$connection_page = $paginator->getPaginate();
		$this->view->setVar('connection_page',$connection_page);



		
	}

	public function threadAction()
	{
		$this->view->setMainView('index');
		$this->view->setVar('page_name', 'Profile');
		$this->view->setVar('content', 'profile/thread');

		$this->initProfileCard();	
	}
	
	public function followedgroupAction()
	{
		$this->view->setVar('content', 'profile/followedgroup');	
	}

	public function mygroupAction()
	{
		$this->view->setVar('content', 'profile/mygroup');	
		
	}


	public function listPendingAction()
	{
		//if($this->request->isPost() == false) die('Not a post');
			$user_id = $this->session->get('user_id');
			$request = ConnectionRequestTb::listRequest($user_id);

			foreach ($request as $value) {
			$data[] = array(
				"id" => $value->crt->sender_id,
				"profile_pic" => $value->uat->profile_pic,
				"fname" => $value->uat->fname,
				"lname" => $value->uat->lname
				);
		}

		echo json_encode($data);
		$this->view->disable();	
	}

}

